36. Validating Salts
Validating Salts
Question:
Start Quiz:
import random
import string
import hashlib
def make_salt():
return ''.join(random.choice(string.letters) for x in xrange(5))
# Implement the function valid_pw() that returns True if a user's password
# matches its hash. You will need to modify make_pw_hash.
def make_pw_hash(name, pw):
salt = make_salt()
h = hashlib.sha256(name + pw + salt).hexdigest()
return '%s,%s' % (h, salt)
def valid_pw(name, pw, h):
###Your code here
#h = make_pw_hash('spez', 'hunter2')
#print valid_pw('spez', 'hunter2', h)
User's Answer:
(Note: The answer done by the user is not guaranteed to be correct)
import random
import string
import hashlib
def make_salt():
return ''.join(random.choice(string.letters) for x in xrange(5))
# Implement the function valid_pw() that returns True if a user's password
# matches its hash. You will need to modify make_pw_hash.
def make_pw_hash(name, pw, salt=''):
if salt == '':
salt = make_salt()
h = hashlib.sha256(name + pw + salt).hexdigest()
return '%s,%s' % (h, salt)
def valid_pw(name, pw, h):
salt = h.split(',')[1]
if make_pw_hash(name, pw, salt) == h:
return True
else:
return False
#h = make_pw_hash('spez', 'hunter2')
#print valid_pw('spez', 'hunter2', h)